package com.meetingshow.privilege.actions;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;

import com.meetingshow.common.Global;
import com.meetingshow.common.base.actions.BaseAction;
import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.privilege.jsonbean.UserJsonBean;
import com.meetingshow.privilege.uiservice.IUIUserService;
import com.meetingshow.privilege.uiservice.model.UserQueryParasVO;
import com.meetingshow.privilege.uiservice.model.UserVO;
import com.meetingshow.utils.JSONUtils;
import com.meetingshow.utils.UIServiceFactory;

@Namespace("/user")
public class UserAction extends BaseAction {

    private final static Logger logger = Logger.getLogger(UserAction.class);
    /**
     * 
     */
    private static final long serialVersionUID = 6809660812568027319L;

    private String param;

    @Action(value = "reg")
    public String regUser() {
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            if (StringUtils.isBlank(param)) {
                response.getWriter().write(buildErrorJson("-1", "No parameter."));
                return null;
            }
            JSONObject jObj = JSONObject.fromObject(param);
            UserJsonBean uJsonBean = (UserJsonBean) JSONObject.toBean(jObj,
                    UserJsonBean.class);

            UserVO userVO = JSONUtils.toUserVO(uJsonBean);
            IUIUserService uiUserService = UIServiceFactory.getUIUserService();
            Integer userId = uiUserService.saveUser(userVO);
            UserJsonBean returnJson = new UserJsonBean();
            if (userId != null)
                returnJson.setUserId(String.valueOf(userId));
            response.getWriter().write(JSONObject.fromObject(returnJson).toString());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try {
                if (e instanceof BaseUIServiceException) {
                    response.getWriter().write(buildErrorJson("-1", e.getMessage()));
                } else {
                    response.getWriter().write(buildErrorJson("-1", "SaveUser error."));
                }
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }
        return null;
    }

    @Action(value = "login")
    public String login() {
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            if (StringUtils.isBlank(param)) {
                response.getWriter().write(buildErrorJson("-1", "No parameter."));
                return null;
            }
            
            JSONObject jObj = JSONObject.fromObject(param);
            UserJsonBean uJsonBean = (UserJsonBean) JSONObject.toBean(jObj,
                    UserJsonBean.class);

            UserQueryParasVO userQueryParasVO = new UserQueryParasVO();
            userQueryParasVO.setUserName_e(uJsonBean.getUserName());
            userQueryParasVO.setPassword_e(uJsonBean.getPassword());

            IUIUserService uiUserService = UIServiceFactory.getUIUserService();
            QueryResults<UserVO> uQueryResults = uiUserService
                    .queryUser(userQueryParasVO);
            if (QueryResults.resultsListNotNull(uQueryResults)) {
                UserVO userVO = uQueryResults.getResultList().get(0);
                ServletActionContext.getRequest().getSession()
                        .setAttribute(Global.SESSION_ATTR_USER, userVO);
            }
            response.getWriter().write(buildSuccJson());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            try {
                if (e instanceof BaseUIServiceException) {
                    response.getWriter().write(buildErrorJson("-1", e.getMessage()));
                } else {
                    response.getWriter().write(buildErrorJson("-1", "SaveUser error."));
                }
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
            }
        }

        return null;
    }

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }
}
